﻿using Furion.FriendlyException;
using ZeroAdmin.Application.Entity;
using ZeroAdmin.Core;
using Mapster;

namespace ZeroAdmin.Application;

/// <summary>
/// 系统配置服务
/// </summary>
public class SysConfigService : ISysConfigService
{
    /// <summary>
    /// 依据条件获取单条系统配置信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SysConfig> Get(QuerySysConfigInput input)
    {
        var obj = await SqlSugarHelper.Queryable<SysConfig>()
            .WhereIF(input.Id != 0, m => m.Id == input.Id)
            .WhereIF(!string.IsNullOrWhiteSpace(input.SysConfigName), m => m.SysConfigName == input.SysConfigName)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), m => m.Code == input.Code)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Value), m => m.Value == input.Value)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Remark), m => m.Remark == input.Remark)
            .FirstAsync();
        _ = obj ?? throw Oops.Bah("指定系统配置不存在");
        return obj;
    }

    /// <summary>
    /// 依据条件获取多条系统配置信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<IEnumerable<SysConfig>> GetList(QuerySysConfigInput input)
    {
        var list = await SqlSugarHelper.Queryable<SysConfig>()
            .WhereIF(input.Id != 0, m => m.Id == input.Id)
            .WhereIF(!string.IsNullOrWhiteSpace(input.SysConfigName), m => m.SysConfigName == input.SysConfigName)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), m => m.Code == input.Code)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Value), m => m.Value == input.Value)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Remark), m => m.Remark == input.Remark)
            .ToListAsync();

        return list;
    }

    /// <summary>
    /// 添加配置信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> Add(EditSysConfigInput input)
    {
        var obj = input.Adapt<SysConfig>();
        try
        {
            long id = (await SqlSugarHelper.InsertReturnEntityAsync(obj)).Id;
            return id;
        }
        catch (Exception ex)
        {
            throw Oops.Bah($"添加配置信息失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 更新配置信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(EditSysConfigInput input)
    {
        var obj = input.Adapt<SysConfig>();
        try
        {
            await SqlSugarHelper.UpdateAsync(obj);
        }
        catch (Exception ex)
        {
            throw Oops.Bah($"编辑配置信息失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 删除配置信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task Delete(long id)
    {
        var obj = await Get(new QuerySysConfigInput { Id = id });
        try
        {
            await SqlSugarHelper.DeleteAsync(obj);
        }
        catch (Exception ex)
        {
            throw Oops.Bah($"删除配置信息失败：{ex.Message}");
        }
    }
}

